<?php
namespace app\home\controller;
use app\common\controller\HomeBase;
use think\Request;
use app\common\model\User as UserModel;
/**
 * 用户个人中心
 * Class User
 * @package app\home\controller
 */
class User extends HomeBase{
    public function index () {
        //获取路由附带到头部的请求参数 单纯使用 $_GET 或 input('get.id') 会获取不到数据的 param 返回的数组
        $uid = Request::instance()->param('uid');
        $user = new UserModel();
        if (!is_object($user->check_id($uid))) {
            $this->redirect('home/login/loginOut');
        }
        $weiboModel = new \app\common\model\Weibo();
        $field = ['username','follow','fans','face180','sex','uid','intro','constellation','location','weibo'];
        $this->assign([
            'follow' => $this->get_data('follow',$uid),
            'fans'  => $this->get_data('fans',$uid),
            'user' => db('userinfo')->where(['uid'=>$uid])->field($field)->find(),
            'uid' => $uid
        ]);
        return $this->fetch('index',['data'=>$weiboModel->getListData(['w.uid'=>$uid])]);
    }

    /**
     * 获取关注和粉丝数据
     * @param null $type
     * @return false|\PDOStatement|string|\think\Collection
     */
    private function get_data($type = 'follow',$uid) {
        if ($type == 'follow') {
            //关注
            $olds = db('follow')->where(['fans'=>$uid])->field('follow')->select();
        } else {
            //粉丝
            $olds = db('follow')->where(['follow'=>$uid])->field('fans')->select();
        }
        $ids = [];
        if ($olds) {
            foreach ($olds as $v) {
                if ($type == 'follow') {
                    $ids[] = $v['follow'];
                } else {
                    $ids[] = $v['fans'];
                }
            }
        } else {
            $ids[] = 0;
        }
        $data = db('userinfo')->where(['uid'=>['in',$ids]])->field(['face50','username','uid'])->select();
        return $data;
    }
}